﻿using System;
using System.Data;
using System.Data.Common;
using Com.Iooogle.Bms.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Com.Iooogle.Bms.Business
{
	/// <summary>
	/// 用户处理业务类。
	/// </summary>
	public class TComments : IBmsBusiness
	{
		#region 标准 IBmsBusiness 成员。

		/// <summary>
		/// 添加记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回最后插入的主键值。</returns>
		public object Create(IDataEntity entity)
		{
			CommentsTable tbl = ( CommentsTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("INSERT INTO `bms_comments`(`BugID`, `Creater`, `Content`, `CreatedTime`) VALUES(@BugID, @Creater, @Content, @CreatedTime);SELECT LAST_INSERT_ID();");
			db.AddInParameter(cmd, "BugID", DbType.Int32, tbl.BugID);
			db.AddInParameter(cmd, "Creater", DbType.Int32, tbl.Creater);
			db.AddInParameter(cmd, "Content", DbType.String, tbl.Content);
			db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, tbl.CreatedTime);
			object obj = db.ExecuteScalar(cmd);

			return obj;
		}

		/// <summary>
		/// 更新记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回受影响的记录条数。</returns>
		public int Update(IDataEntity entity)
		{
			CommentsTable tbl = ( CommentsTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("UPDATE `bms_comments` SET `BugID` = @BugID, `Creater` = @Creater, `Content` = @Content, `CreatedTime` = @CreatedTime WHERE `CoID` = @CoID");
			db.AddInParameter(cmd, "BugID", DbType.Int32, tbl.BugID);
			db.AddInParameter(cmd, "Creater", DbType.Int32, tbl.Creater);
			db.AddInParameter(cmd, "Content", DbType.String, tbl.Content);
			db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, tbl.CreatedTime);
			db.AddInParameter(cmd, "CoID", DbType.Int32, tbl.CoID);
			int obj = db.ExecuteNonQuery(cmd);

			return obj;
		}

		/// <summary>
		/// 删除记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		public void Delete(object key)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("DELETE FROM `bms_comments` WHERE `CoID` = @CoID");
			db.AddInParameter(cmd, "CoID", DbType.Int32, key);
			db.ExecuteNonQuery(cmd);
		}

		/// <summary>
		/// 获取全部记录。
		/// </summary>
		/// <returns></returns>
		public DataSet GetAll()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `CoID`, `BugID`, `Creater`, `Content`, `CreatedTime` FROM `bms_comments`");
			DataSet ds = db.ExecuteDataSet(cmd);

			return ds;
		}

		/// <summary>
		/// 获取数据总数。
		/// </summary>
		/// <returns></returns>
		public int GetCount()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(*) FROM `bms_comments`");
			Int32 obj = Convert.ToInt32(db.ExecuteScalar(cmd));

			return obj;
		}

		/// <summary>
		/// 获取单行记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		/// <returns></returns>
		public IDataEntity GetRow(object key)
		{
			CommentsTable entity = null;
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `CoID`, `BugID`, `Creater`, `Content`, `CreatedTime` FROM `bms_comments` WHERE `CoID` = @CoID");
			db.AddInParameter(cmd, "CoID", DbType.Int32, key);
			using (IDataReader dr = db.ExecuteReader(cmd))
			{
				if (dr.Read())
				{
					entity = new CommentsTable();
					entity.CoID = Convert.ToInt32(dr["CoID"]);
					entity.BugID = Convert.ToInt32(dr["BugID"]);
					entity.Creater = Convert.ToInt32(dr["Creater"]);
					entity.Content = Convert.ToString(dr["Content"]);
					if (!Convert.IsDBNull(dr["CreatedTime"]))
						entity.CreatedTime = Convert.ToDateTime(dr["CreatedTime"]);

					dr.Close();
				}
			}

			return entity;
		}

		#endregion
	}
}